package com.selune.online.ordering.system.common.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.base.Throwables;
import com.selune.online.ordering.system.project.api.base.entity.CustomerUserInfo;
import com.selune.online.ordering.system.project.api.base.entity.TenantUserInfo;
import com.selune.online.ordering.system.project.api.base.exception.ParamsException;
import com.selune.online.ordering.system.project.api.base.exception.UserException;
import com.selune.online.ordering.system.project.api.base.response.Response;
import com.selune.online.ordering.system.project.api.mapper.CustomerUserInfoMapper;
import com.selune.online.ordering.system.project.api.mapper.TenantUserInfoMapper;
import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 *
 * @author xiaoyp
 * @date 2020/3/23
 * <p>
 * Description:
 */
@Slf4j
public class FindUserUtils {
	
	public static Response<CustomerUserInfo> getCustomerUserById(CustomerUserInfoMapper customerUserInfoMapper,
	                                                         Long userId) {
		if (null == userId) {
			log.error("ACCESS_LOG[]FindUserUtils[]getCustomerUserById[]user_id is null!");
			throw new ParamsException("user.id.is.null");
		}
		CustomerUserInfo customerUserInfo;
		
		try {
			customerUserInfo = customerUserInfoMapper.selectById(userId);
		} catch (Exception e) {
			log.error("ACCESS_LOG[]FindUserUtils[]getCustomerUserById[]user is not exist! cause:{}",
				Throwables.getStackTraceAsString(e));
			throw new UserException("user.is.not.exist");
		}
		
		if (null == customerUserInfo) {
			log.error("ACCESS_LOG[]FindUserUtils[]getCustomerUserById[]user is not exist!");
			throw new UserException("user.is.not.exist");
		}
		
		return Response.success(customerUserInfo);
	}
	
	public static Response<CustomerUserInfo> getCustomerByUserName(CustomerUserInfoMapper customerUserInfoMapper,
	                                                               String userName) {
		if (StringUtils.isBlank(userName)) {
			log.error("ACCESS_LOG[]FindUserUtils[]getCustomerByUserName[]user name is null!");
			throw new ParamsException("user.name.is.null");
		}
		CustomerUserInfo customerUserInfo;
		
		try {
			customerUserInfo = customerUserInfoMapper.selectOne(
				Wrappers.<CustomerUserInfo>lambdaUpdate().eq(CustomerUserInfo::getUserName, userName)
			);
		} catch (Exception e) {
			log.error("ACCESS_LOG[]FindUserUtils[]getCustomerByUserName[]user name is not exist! cause:{}",
				Throwables.getStackTraceAsString(e));
			throw new UserException("user.is.not.exist");
		}
		
		return Response.success(customerUserInfo);
	}
	
	public static Response<TenantUserInfo> getTenantUserById(TenantUserInfoMapper tenantUserInfoMapper, Long tenantId) {
		if (null == tenantId) {
			log.error("ACCESS_LOG[]FindUserUtils[]getTenantUserById[]tenant_id is null!");
			return Response.error("tenant.id.is.null");
		}
		TenantUserInfo tenantUserInfo;
		
		try {
			tenantUserInfo = tenantUserInfoMapper.selectById(tenantId);
		} catch (Exception e) {
			log.error("ACCESS_LOG[]FindUserUtils[]getTenantUserById[]tenant_id is not exist! cause:{}",
				Throwables.getStackTraceAsString(e));
			throw new UserException("tenant.is.not.exist");
		}
		
		if (null == tenantUserInfo) {
			log.error("ACCESS_LOG[]FindUserUtils[]getTenantUserById[]tenant is not exist!");
			throw new UserException("tenant.is.not.exist");
		}
		
		return Response.success(tenantUserInfo);
	}
	
	public static Response<TenantUserInfo> getTenantByTenantName(TenantUserInfoMapper tenantUserInfoMapper, String tenantName) {
		if (StringUtils.isBlank(tenantName)) {
			log.error("ACCESS_LOG[]FindUserUtils[]getTenantByTenantName[]tenant_name is null!");
			return Response.error("tenant.name.is.null");
		}
		
		TenantUserInfo tenantUserInfo = tenantUserInfoMapper.selectOne(
			Wrappers.<TenantUserInfo>lambdaUpdate().eq(TenantUserInfo::getTenantName, tenantName)
		);
		
		return Response.success(tenantUserInfo);
	}
}
